import path from 'path'
import fsExtra from 'fs-extra'

const {existsSync, remove, copy} = fsExtra
const cwd = process.cwd()
const releaseRelativePath = '../server/public/'
const distPath = path.resolve(cwd, 'dist')
const releasePath = path.resolve(cwd, releaseRelativePath)
const releaseDistPath = path.resolve(releasePath, 'assets')

async function build() {
    if (existsSync(releaseDistPath)) {
        await remove(releaseDistPath)
    }

    try {
        await copyFile(distPath, releasePath)
    } catch (error) {
        console.log(`\n ${error}`)
    }
    console.log(`${releasePath} copy over.`)
}

function copyFile(sourceDir, targetDir) {
    return new Promise((resolve, reject) => {
        copy(sourceDir, targetDir, (err) => {
            if (err) {
                reject(err)
            } else {
                resolve()
            }
        })
    })
}

build()